package com.example.gamecaro;



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;


import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class Game extends View {
	private Cell [][]ChessBoard=null;
	int x=20;								//chiều ngang
	int y=20;		//chiều dọc
	private Paint paint;
	private boolean whatdraw=false;
	int with, height;
	public Game(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		paint=new Paint();
		this.paint.setARGB(255, 0, 0, 0);
		this.paint.setAntiAlias(true);
		this.paint.setStyle(Style.STROKE);
		this.paint.setStrokeWidth(5);
		 with=this.getWidth()/x;
		 height=this.getHeight()/y;
		ChessBoard=new Cell[x][y];
		for(int i=0;i<y;i++){
			for(int j=0;j<x;j++){
				ChessBoard[i][j]=new pieces_empty(with*j, height*i);
			}
		}
		
	
	}
	protected void onDraw(Canvas canvas){
		for(int i=0;i<ChessBoard.length;i++){
			for(int j=0;j<ChessBoard[0].length;j++){
				ChessBoard[i][j].draw(canvas,getResources(), j, i,(this.getWidth()+3)/ChessBoard.length,(this.getHeight())/ChessBoard[0].length);
			}
		}
		super.onDraw(canvas);
	}
	public boolean onTouchEvent(MotionEvent event){
		int point_x=(int)(event.getX()/(this.getWidth()/x));
		int point_y=(int)(event.getY()/(this.getHeight()/y));
		drawpieces(point_x,point_y);
		return super.onTouchEvent(event);
	}
	private void drawpieces(int point_x,int point_y) {
		// TODO Auto-generated method stub
		Cell cell=null;
		if(whatdraw)
		{
			if(!Empty_Check(point_x, point_y))
				return;
			cell=new pieces_o(ChessBoard[point_x][point_y].x,ChessBoard[point_x][point_y].y);
			whatdraw=false;
			
		}
		else
		{
			if(!Empty_Check(point_x, point_y))
				return;
			cell=new pieces_x(ChessBoard[point_x][point_y].x,ChessBoard[point_x][point_y].y);
			whatdraw=true;
		}
		ChessBoard[point_y][point_x]=cell;
		invalidate();
		if(Check_Win())
		{
			if(whatdraw)
				Toast.makeText(getContext(),"You Win", Toast.LENGTH_SHORT).show();
			else
				Toast.makeText(getContext(),"Computer Win", Toast.LENGTH_SHORT).show();
			resizeGame();
		}
	}

	private boolean Check_Win() {
		// TODO Auto-generated method stub
		int dem=0;
		Cell temp=null;
		for(int i=0;i<ChessBoard.length;i++){
			for(int j=0;j<ChessBoard[0].length;j++){
				System.out.println(ChessBoard[i][j]);
				
				if (!(ChessBoard[i][j].equals(temp)) || ChessBoard[i][j]instanceof pieces_empty) {
					temp=ChessBoard[i][j];
					System.out.println(temp);
					dem=0;
				} else {
					dem++;
				}
				if(dem>=4)
					return true;
				
			}
			temp=null;
			dem=0;
		}
		temp=null;
		for(int j=0;j<ChessBoard[0].length;j++){
			for(int i=0;i<ChessBoard.length;i++){
				if(!(ChessBoard[i][j].equals(temp)) || ChessBoard[i][j]instanceof pieces_empty)
				{
					temp=ChessBoard[i][j];
					dem=0;
				}
				else
					dem++;
				if(dem>=4)
					return true;
			}
			temp=null;
			dem=0;
		}
		temp=null;
		for(int j=ChessBoard[0].length-1;j>=0;j--){
			int k=0;
			for(int z=j;z<ChessBoard[0].length;z++){
				if(!ChessBoard[k][z].equals(temp)|| ChessBoard[k][z] instanceof pieces_empty){
					temp=ChessBoard[k][z];
					dem=0;
				}
				else
					dem++;
				if(dem>=4)
					return true;
				k++;
			}
			temp=null;
			dem=0;
		}
		temp=null;
		for(int j=0;j<ChessBoard[0].length;j++){
			int k=0;
			for(int z=j;z>=0;z--){
				if(!ChessBoard[k][z].equals(temp)|| ChessBoard[k][z] instanceof pieces_empty){
					temp=ChessBoard[k][z];
					dem=0;
				}
				else
					dem++;
				if(dem>=4)
					return true;
				k++;
			}
			temp=null;
			dem=0;
		}		
		
		return false;
	}
	private boolean Empty_Check(int point_x,int point_y)
	{
		if(ChessBoard[point_y][point_x]instanceof pieces_empty )
			return true;
		return false;
	}
	private void resizeGame()
	{
		for(int i=0;i<y;i++){
			for(int j=0;j<x;j++){
				ChessBoard[i][j]=new pieces_empty((this.getWidth()/x)*j, (this.getHeight()/y)*i);
			}
		}
		invalidate();
	}
}
